package org.worthydots.access.jpa.dao;

import java.util.List;

import org.worthydots.access.jpa.entity.Station;

class StationDAOImpl extends DAOImpl implements StationDAO {

	public StationDAOImpl() {
		super();
	}

	public void saveStation(Station station) {
		entityManager.getTransaction().begin();
		entityManager.merge(station);
		entityManager.getTransaction().commit();
	}

	public void saveStationIfNotPresent(Station station) {
		Station stationInDB = getStationByStationCode(station.getCode());
		if (stationInDB == null)
			saveStation(station);
	}

	public Station getStationByStationCode(String stationCode) {
		return entityManager.find(Station.class, stationCode);
	}

	@SuppressWarnings("unchecked")
	public List<Station> getAllStations() {
		return (List<Station>) entityManager.createQuery("SELECT si FROM Station si order by si.code").getResultList();
	}
}
